package lab2task3;

/**
 * 
 * Main class of circular buffer implementation 
 *  without using java.util.concurrent
 * 
 * @author Bulaev Igor
 *
 */

public class Main {

	private BoundedBuffer buffer;

	public static void main(String[] args) {
		new Main().init();
	}

	private void init() {
		buffer = new BoundedBuffer(4);
		new Thread(new ReaderRunnable()).start();
		new Thread(new WriterRunnable()).start();
	}
	
	/**
	 * 
	 * Reader runnable for testing purposes
	 * 
	 * @author Bulaev Igor
	 *
	 */

	private class ReaderRunnable implements Runnable {

		@Override
		public void run() {
			while (true) {
				try {
					buffer.take();
					Thread.sleep((long) (Math.random() * 1500));
				} catch (InterruptedException ie) {
					ie.printStackTrace();
				}
			}
		}

	}

	/**
	 * 
	 * Writer runnable for testing purposes
	 * 
	 * @author Bulaev Igor
	 *
	 */
	
	private class WriterRunnable implements Runnable {

		@Override
		public void run() {
			while (true) {
				try {
					buffer.put('c');
					Thread.sleep((long) (Math.random() * 1500));
				} catch (InterruptedException ie) {
					ie.printStackTrace();
				}
			}
		}

	}

}
